package com.framework.web.sys.controller;


import com.framework.core.constant.Globals;
import com.framework.core.domain.AjaxJson;
import com.framework.core.easyui.domain.DataGridModel;
import com.framework.core.easyui.util.PageUtil;
import com.framework.core.util.StringUtil;
import com.framework.web.common.mappings.SysUrlMappings;
import com.framework.web.sys.domain.CodeItems;
import com.framework.web.sys.service.CodeItemsService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 资源信息管理——控制层
 *
 * @author fusf
 */
@Controller
public class CodeItemsController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    /**
     * Spring注入资源信息模块service
     */
    @Autowired
    private CodeItemsService codeItemsService;


    /**
     * 获取基础数据类型列表
     *
     * @param dgm
     * @param codeItems
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_CODE_ITEMS_LIST, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(DataGridModel dgm, CodeItems codeItems, HttpServletRequest request) throws Exception {
        // 通过easyui分页控件获取page和rows数据，添加分页信息
        PageRequest pr = DataGridModel.getPageRequest(dgm);
        return PageUtil.getPageMap(codeItemsService.getPageList(codeItems, pr));
    }

    @RequestMapping(value = SysUrlMappings.SYS_CODE_ITEMS_TOSAVEANDUPDATE, method = RequestMethod.GET)
    public ModelAndView toSaveAndUpdate(@PathVariable("id") String id,@PathVariable("codeCategoryId") String  codeCategoryId)
            throws Exception {
        // 进入基础数据类型添加或修改页面
        ModelAndView modelAndView = new ModelAndView(SysUrlMappings.V_SYS_CODE_ITEMS_SAVE_UPDATE);
        CodeItems codeItems = new CodeItems();
        // 根据传入id值，判断修改还是新增信息，并新增初始化数据是否可用为"T"。
        if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
            // 得到需要修改的数据类型实体
            codeItems = codeItemsService.findById(id);
        } else {
            // 新增对象初始化是否可用字段
            codeItems.setStatus(Globals.STATUS_YES);
            codeItems.setCodeCategoryId(codeCategoryId);
        }
        // 将对象放入Model中返回到页面
        modelAndView.addObject("codeItems", codeItems);
        return modelAndView;
    }

    /**
     * 保存
     *
     * @param request
     * @param response
     * @param CodeItems
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_CODE_ITEMS_SAVEANDUPDATE, method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson CodeItemsSaveAndUpdate(HttpServletRequest request, HttpServletResponse response, CodeItems CodeItems)
            throws Exception {
        AjaxJson j = new AjaxJson();
        boolean bValidate = this.validateCodeItems(CodeItems);
        if (bValidate) {
            j.setSuccess(false);
            j.setMsg("类别code重复");
        } else {
           codeItemsService.save(CodeItems);//保存资源信息
            j.setMsg("类别保存成功");
        }
        return j;
    }


    private boolean validateCodeItems(CodeItems CodeItems) {
        boolean bValidate = false;
        //查询名字是否重复
      /*  List<CodeItems> CodeItemsList = CodeItemsService.findByCategoryCodeAndOpenAccountId(CodeItems.getCategoryCode(), CodeItems.getOpenAccountId());
        if (CodeItemsList != null && CodeItemsList.size() > 0) {
            if (CodeItemsList.size() > 1 || CodeItems.getId() == null || CodeItems.getId() == 0) { //新增状态
                bValidate = true;
            } else {
                if (CodeItems.getId() != CodeItemsList.get(0).getId()) {
                    bValidate = true;
                }
            }
        }*/
        return bValidate;
    }


    @RequestMapping(value = SysUrlMappings.SYS_CODE_ITEMS_DELETE, method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson CodeItemsDelete(String id)
            throws Exception {
        AjaxJson j = new AjaxJson();
        codeItemsService.delete(id);
        j.setMsg("删除成功");
        return j;
    }

    /**
     * 字典查询
     * @param categoryCode
     * @param code
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_CODE_ITEMS_BYCATEGORYCODEANDCODE)
    @ResponseBody
    public AjaxJson getCodeItemsByCategoryCodeAndValue(String categoryCode,String code) throws Exception {
        AjaxJson j = new AjaxJson();
        //String name = codeItemsService.findCodeItemsNameByCategoryCodeAndCode(categoryCode, code);
        List<CodeItems> codeItemsList = codeItemsService.getListByCategoryCode(categoryCode);
        String name = "";
        for(CodeItems codeItems :codeItemsList) {
            if(codeItems.getCode().equals(code)) {
                name = codeItems.getName();
                break;
            }
        }
        j.setObj(name);
        return j;
    }

    /**
     * 获取字典表信息
     * @param request
     * @param response
     * @param categoryCode
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_CODE_ITEMS_LIST_BYCATEGORYCODE)
    @ResponseBody
    public List<CodeItems> getCodeItemsByCategoryCode(HttpServletRequest request,HttpServletResponse response,String categoryCode) throws Exception {
        return codeItemsService.getListByCategoryCode(categoryCode);
    }

    /**
     * 缓存刷新
     * @return
     * @throws Exception
     */
    @RequestMapping(value = SysUrlMappings.SYS_CODE_ITEMS_REFRESHCACHE)
    @ResponseBody
    public AjaxJson refreshCache() throws Exception {
        AjaxJson j = new AjaxJson();
        codeItemsService.refreshCache();
        j.setMsg("缓存刷新成功");
        return j;
    }

}